package com.outfood.utils.profitshare;

import com.alibaba.fastjson.JSON;
import com.outfood.config.WxConfig;
import com.outfood.domain.User;
import com.outfood.service.IUserService;
import com.outfood.utils.UuidUtils;
import com.tps.open.weixin.dto.AddReceiverDTO;
import com.tps.open.weixin.dto.PaymentDTO;
import com.tps.open.weixin.open.SubmitPaymentService;
import com.tps.open.weixin.vo.PaymentVO;
import com.tps.open.weixin.vo.ReceiverVO;
import com.tps.open.weixin.vo.XcxPayNotifyInfoVO;
import lombok.extern.log4j.Log4j2;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author huisonglin
 * @Description
 * @PackageName outfood-platform
 * @Package com.outfood.utils.profitshare
 * @Date 2024-05-09 17:38
 * @Version 1.0
 */
@Component
@Log4j2
public class ShareUtil {

    @Reference
    SubmitPaymentService submitPaymentService;
    @Resource
    WxConfig wxConfig;
    @Resource
    IUserService userService;

//    @PostConstruct
//    public void test(){
//        XcxPayNotifyInfoVO xcxPayNotifyInfoVO = new XcxPayNotifyInfoVO();
//        xcxPayNotifyInfoVO.setTotal_fee("50");
//        xcxPayNotifyInfoVO.setTransaction_id("4200002148202405095214954429");
//        share(xcxPayNotifyInfoVO,20L);
//    }

    public void share(XcxPayNotifyInfoVO xcxPayNotifyInfoVO,Long refereeId){
        //添加分账接受方
        User user = userService.get(refereeId);
        AddReceiverDTO addReceiverDTO = new AddReceiverDTO();
        addReceiverDTO.setSub_mchid(wxConfig.getWxMchId());
        addReceiverDTO.setAppid(wxConfig.getWxAppid());
        addReceiverDTO.setKey(wxConfig.getWxKey());
        AddReceiverDTO.Receiver receiver = new AddReceiverDTO.Receiver();
        receiver.setType("PERSONAL_OPENID");
        receiver.setAccount(user.getOpenId());
        receiver.setRelation_type("USER");
        addReceiverDTO.setReceiver(receiver);
        String s1 = submitPaymentService.addReceiver(addReceiverDTO);
        log.info("添加收款方结果为{}",s1);

        //开始分账
        String total_fee = xcxPayNotifyInfoVO.getTotal_fee();
        int fee = new BigDecimal(total_fee).multiply(new BigDecimal(0.28)).intValue();
        PaymentDTO paymentDTO = new PaymentDTO();
        paymentDTO.setNonce_str(UuidUtils.get32UUID());
        paymentDTO.setTransaction_id(xcxPayNotifyInfoVO.getTransaction_id());
        paymentDTO.setOut_order_no( UuidUtils.generateOrderNo("FX"));
        paymentDTO.setMchId(wxConfig.getWxMchId());
        paymentDTO.setAppid(wxConfig.getWxAppid());
        paymentDTO.setKey(wxConfig.getWxKey());
        ReceiverVO receiverVO = new ReceiverVO();
        receiverVO.setAmount(fee);

        receiverVO.setAccount(user.getOpenId());
        receiverVO.setType("PERSONAL_OPENID");
        receiverVO.setDescription("被推荐人已续费，您获取续费金额的百分之30提成");
        List<ReceiverVO> vos = new ArrayList<ReceiverVO>();
        vos.add(receiverVO);
        paymentDTO.setReceiverVO(JSON.toJSONString(vos));
        log.info(JSON.toJSONString(paymentDTO));
        String s = submitPaymentService.executePayment(paymentDTO);
        log.info("分账结果为{}",s);
        log.info(s);
        //System.out.println(JSON.toJSONString(executePayment));
    }
}
